package com.threeti.mecool.core.domain.model.message;

import com.threeti.mecool.core.infrastructure.persistence.message.MobileMessageRepository;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/** 手机推送消息 */
@Configurable
@Entity
@Table(name = "DM_MOBILE_MESSAGE")
public class MobileMessage {
    private Long id;
    private Integer version;
    private Long createdOn;
    private String creator;
    private Priority priority;
    private String head;
    private String body;
    private String receiver;
    private Long receivedOn;
    private MobileMessageRepository repository;

    public static enum Priority {
        NORMAL,
        URGENT,
        ASSIGN;
    }

    @Transactional
    public void save() {
        repository.save(this);
    }

    //<editor-fold desc="static find">
    private static MobileMessageRepository repository() {
        return new MobileMessage().repository;
    }

    public static MobileMessage findById(Long id) {
        if (id == null) {
            return null;
        } else {
            return repository().findOne(id);
        }
    }
    //</editor-fold>

    //<editor-fold desc="constructor">
    public MobileMessage() {
        this.createdOn = new Date().getTime();
        this.priority = Priority.NORMAL;
    }

    public MobileMessage(String head, String body, String receiver, String creator) {
        this();
        this.head = head;
        this.body = body;
        this.receiver = receiver;
        this.creator = creator;
    }

    public MobileMessage(String head, String body, String receiver, String creator, Priority priority) {
        this(head, body, receiver, creator);
        this.priority = priority;
    }
    //</editor-fold>

    //<editor-fold desc="get/set">
    @Resource
    public void setRepository(MobileMessageRepository repository) {
        this.repository = repository;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    @NotNull
    public String getCreator() {
        return creator;
    }

    @NotNull
    public Long getCreatedOn() {
        return createdOn;
    }

    @NotNull
    public String getReceiver() {
        return receiver;
    }

    public Long getReceivedOn() {
        return receivedOn;
    }

    @NotNull
    public String getHead() {
        return head;
    }

    @NotNull
    public String getBody() {
        return body;
    }

    @NotNull
    @Enumerated(EnumType.STRING)
    public Priority getPriority() {
        return priority;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public void setCreatedOn(Long createdOn) {
        this.createdOn = createdOn;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public void setReceivedOn(Long receivedOn) {
        this.receivedOn = receivedOn;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public void setPriority(Priority priority) {
        this.priority = priority;
    }
    //</editor-fold>

    @Transient
    @JsonIgnore
    public Date getCreatedOnDate(){
    	if(createdOn!=null){
    		return new Date(createdOn);
    	}else{
    		return null;
    	}
    }

    @Transient
    @JsonIgnore
    public Date getReceivedOnDate(){
    	if(receivedOn!=null){
    		return new Date(receivedOn);
    	}else{
    		return null;
    	}
    }
}
